<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../../css/mui.min.css" rel="stylesheet" />
		<link rel="stylesheet" href="../../css/header.css" />
	</head>

	<body>
		
		<header class="mui-bar mui-bar-nav title">
		    <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left title-color"></a>
		    <h1 class="mui-title title-color">详细资料</h1>
		</header>
		
		<div class="mui-content">
		    <ul class="mui-table-view">
		        <li class="mui-table-view-cell mui-media">
		            <a href="javascript:;">
		                <img id="imgCon" class="mui-pull-left" style="width: 50px;height: 50px;border-radius: 5px;" src=""/>
		                <div class="mui-media-body" style="line-height: 60px;font-size: 16px;">
		                	<label style="margin-left: 10px;" id="mark"></label>
		                	<img id="gender" src="" style="width: 25px; height: 25px;" />
		                </div>
		            </a>
		        </li>
		    </ul>
		    <br />
		    <ul class="mui-table-view">
		        <li class="mui-table-view-cell mui-media">
	                <div class="mui-media-body">
	                                    嗨聊账号
	                    <label id="username" style="padding-left: 20px;">ssss</label>
	                </div>
		        </li>
		    </ul>
		    
		    <div style="width: 100%;text-align: center;margin-top: 30px;" id="button_friend">
		    </div>
		</div>
		
		<script src="../../js/mui.min.js"></script>
		<script type="text/javascript" src="../../js/app.js" ></script>
		<script type="text/javascript">
			mui.init();
			
			mui.plusReady(function(){
				
				plus.navigator.setStatusBarStyle("light");
	      		plus.navigator.setStatusBarBackground("#4eabe8");
				
				var self = plus.webview.currentWebview();
				var imgCon = self.imgCon;
				var markName = self.markName;
				var username = self.username;
				var nickname = self.nickname;
				var gender = self.gender;
				var friend = self.friend;
				var friendID = self.friendId;
				
				var userGender = document.getElementById("gender");
				if(gender == "男") {
					userGender.src = "../../img/man.png";
				}else{
					userGender.src = "../../img/woman.png";
				}
				
				document.getElementById("imgCon").src = imgCon;
				document.getElementById("username").innerHTML = username;
				document.getElementById("mark").innerHTML = markName;
				
				var button_friend = document.getElementById("button_friend");
				if(friend) {
					button_friend.innerHTML = "<button type='button' style='background-color: #4eabe8;width:90%;height: 50px;font-size: 18px;border: none;color: white;' id='button_friend_msg'><b>发&nbsp;送&nbsp;消&nbsp;息</b></button>";
					var button_friend_msg = document.getElementById("button_friend_msg");
					button_friend_msg.addEventListener("tap", function(){
                		//chatMsg(friendId, markName, imgCon);
                		mui.openWindow({
							url:'../chat/feixin_chat.html', 
							id:"chatting-" + friendID,
							extras:{
								friendId: friendID,
								friendMarkName: markName == null ? nickname: markName,
								friendAvatar: imgCon
							}
						});
                	});
				}else{
					button_friend.innerHTML = "<button type='button' style='background-color: #4eabe8;width:90%;height: 50px;font-size: 18px;border: none;color: white;' id='button_friend_add'><b>添&nbsp;加&nbsp;好&nbsp;友</b></button>";
					//添加好友
					var button_friend_add = document.getElementById("button_friend_add");
					button_friend_add.addEventListener("tap", function(){
                		sendFriendRequest(username);
                	});
				}
                
                
                //打开聊天界面
                //document.getElementById("button_friend_msg").addEventListener("tap", function(){});
                
                //显示当前页面
                //mui.currentWebview.show();
			});
			
			function sendFriendRequest(username){
				plus.nativeUI.showWaiting("请稍后...");
				mui.ajax(app.serverUrl + "/simple-chat/api/friends/add",{
					data:{
						senderId: app.getUserInfo().userId,
						friendUsername: username
					},
					dataType:'json',
					type:'post',
					timeout:10000,
					headers:{'Authorization':'Bearer ' + app.getToken()},           
					success:function(data){
						//服务器返回响应
						plus.nativeUI.closeWaiting();
						
						if(data.status == 0) {
							app.showToast("好友申请已发送", "success");
							
							mui.openWindow({
								url: "../../index.html",
								id: "index.html"
							});
						}else if(data.status == 6001) {
							app.showToast("该用户不存在", "error");
						}else if(data.status == 6004) {
							app.showToast("他们已经是好友", "error");
						}else {
							app.showToast("网络异常，请检查您的网络", "error");
						}
					},
					error:function(xhr,type,errorThrown){
						//异常处理；
						app.showToast("网络异常，请检查您的网络", "error");
					}
				});
			}
			
			function chatMsg(friendId, friendMarkName, friendAvatar) {
				//打开聊天信息界面
				//app.showToast("这里将打开俩天界面", "success");
//				
//				mui.openWindow({
//					url:'../chat/feixin_chat.html', 
//					id:"chatting-" + friendId,
//					extras:{
//						friendId: friendId,
//						friendMarkName: friendMarkName,
//						friendAvatar: friendAvatar
//					}
//				});
			}
		</script>
	</body>

</html>